package com.example.lin.jd_banner.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.lin.jd_banner.R;
import com.example.lin.jd_banner.pojo.BannerData;
import com.squareup.picasso.Picasso;

import java.util.List;

public class BannerPagerAdapter extends PagerAdapter {

    private Context context;
    private BannerData data;

    public BannerPagerAdapter(Context context) {
        this.context = context;
    }

    public void setData(BannerData data) {
        this.data = data;
        notifyDataSetChanged();//刷新适配器
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.getParamz().getTops().size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_banner_item_1, container, false);
        ImageView iv_item = view.findViewById(R.id.iv_item);
        TextView tv_subject = view.findViewById(R.id.tv_subject);

        //获取该位置的对象数据
        BannerData.ParamzBean.TopsBean topsBean = data.getParamz().getTops().get(position);
        tv_subject.setText(topsBean.getSubject());

        Glide.with(context).load(topsBean.getPhoto()).into(iv_item);
        Picasso.get().load(topsBean.getPhoto()).into(iv_item);

        container.addView(view);

        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }

}
